package com.hang.controller;

import com.hang.common.Result;
import com.hang.config.MsmConfig;
import com.hang.config.RedisConfig;
import com.hang.exception.MyException;
import com.hang.model.dto.request.LoginModel;
import com.hang.service.ILoginService;
import com.hang.service.IMsmService;
import com.hang.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "登录管理模块")
@RequestMapping("login")
@Slf4j
public class LoginController {
    @Resource
    private ILoginService loginService;

    @Resource
    private MsmConfig msmConfig;

    @Resource
    private RedisConfig redisConfig;

    @Resource
    private IMsmService msmService;

    @PostMapping("loginByPassword")
    @ApiOperation(value = "用户名密码登录")
    public Result loginByPassword(
            @RequestBody @ApiParam(value = "登录实体对象") LoginModel loginModel
    ){
        Map<String,Object> map;
        try {
            map = loginService.loginByPwd(loginModel);
        }catch (MyException me){
            log.error("fail: "+me.toString());
            return Result.fail().message(me.getMessage());
        }
        return Result.ok(map);
    }

    @GetMapping("send")
    @ApiOperation(value = "发送手机验证码")
    public Result sendCode(
            @RequestParam("phone") @ApiParam(value = "手机号") String phone
    ){
        RedisTemplate<String, Object> redisTemplate = redisConfig.getRedisTemplateByDb(0);

        boolean exist = msmService.codeExist(phone);
        if(exist){
            //验证码不为空，返回结果即可
            return Result.fail().message("已存在验证码");
        }

        //如果为空，那么随机生成验证码，并发送短信
        String random = msmConfig.printRandom();

        boolean sendSuccess = msmService.send(phone,random);

        if(sendSuccess){
            //发送成功
            redisTemplate.opsForValue().set(phone,random,5, TimeUnit.MINUTES);
            return Result.ok();
        }else {
            return Result.fail().message("发送短信失败,请稍后重试~");
        }
    }

    @PostMapping("loginByTel")
    @ApiOperation(value = "通过手机验证码登录")
    public Result loginByTelPhone(
            @RequestBody @ApiParam(value = "登录实体对象") LoginModel loginModel
    ){
        Map<String,Object> map;
        try {
            map = loginService.loginByTel(loginModel);
        }catch (MyException me){
            log.error("fail: "+me.toString());
            return Result.fail().message(me.getMessage());
        }
        return Result.ok(map);
    }

    @GetMapping("getUserId")
    @ApiOperation(value = "通过token获取用户ID")
    public Result<Long> getUserIdByToken(String token){
        return Result.ok(JwtUtil.getUserId(token));
    }

    @GetMapping("getRealName")
    @ApiOperation(value = "通过token获取用户的姓名")
    public Result<String> getRealNameByToken(String token){
        return Result.ok(JwtUtil.getUserName(token));
    }
}